from django.core.cache import cache
from django.core.paginator import Paginator
from django.shortcuts import render
from app.models import *
from rest_framework.views import APIView
from rest_framework.response import Response
from celery_tasks.sms.tasks import ccp_send_sms_code

# Create your views here.
class PostView(APIView):
    # 添加帖子
    def post(self, request):
        print(111)
        PostModel.objects.create(
            title=request.data.get('title'),
            content=request.data.get('content'),
            user_id=request.data.get('user_id')
        )
        try:
            data = {
                'title': request.data.get("title"),
                'content': request.data.get('content'),
                'user_id': request.data.get('user_id')
            }
            # 异步
            ccp_send_sms_code.delay()
            return Response({
                'code':200,
                'message':'添加成功',
                'data':data
            })
        except Exception as e:
            return Response({
                'code': 200,
                'message': '添加失败',
            })


class UserView(APIView):
    # 获取帖子列表
    def get(self,request):
        # sql = "select * from user where order by id desc "
        # qs = UserView.objects.raw(sql)
        sql = "select * from post where user_id=2 order by id desc "
        post_id = PostModel.objects.raw(sql)
        # user_id = UserModel.objects.filter(id=id).first()
        # post_id = user_id.postmodel_set.all()
        post_list = []
        for i in post_id:
            post_list.append(
                {
                    'id':i.id,
                    'title':i.title,
                    'content':i.content,

                }
            )
        data = {
            # 'id':user_id.id,
            # 'username':user_id.username,
            # 'email':user_id.email,
            # 'possword':user_id.password,
            'post':post_list

        }

        ccp_send_sms_code.delay()
        return Response({
            'code':200,
            'message':'查询成功',
            'data':data
        })


class PosttView(APIView):
    # 4、redis缓存
    def get(self,request):
        data_wwh = cache.get('wwh')
        if data_wwh:
            print('从redis缓存中成功读取数据成功')
            return Response({'code': 200, 'errmsg': 'OK', 'data': data_wwh})
        post = PostModel.objects.all()
        post_list = []
        for i in post:
            post_list.append(
                {
                    'id': i.id,
                    'title': i.title,
                    'content': i.content,
                    'user_id':i.user_id
                }
            )
        cache.set('wwh', post_list, 60)
        print('redis缓存中没有数据,从数据库中读取数据')
        return Response({
            'code':200,
            'message':"查询成功",
            'data':post_list
        })


class PostttView(APIView):
    def get(self,request):
        current_page = request.query_params.get('page')
        qs = PostModel.objects.all()
        # qs = SjhModel.objects.all()
        paginator = Paginator(qs, 4)
        try:
            page_qs = paginator.page(current_page)
        except Exception as e:
            print(e)
            page_qs = paginator.page(1)
        post_list = []
        for i in page_qs:
            post_list.append(
                {
                    'id': i.id,
                    'title': i.title,
                    'content': i.content,
                    'user_id': i.user_id
                }
            )
        return Response({
            'code': 200,
            'message': "分页查询成功",
            'data': post_list
        })

class PosttttView(APIView):
    def get(self,request):
        sql = "select * from post where user_id=1 limit 10"
        qs = PostModel.objects.raw(sql)
        # qs = post.user.row(sql)
        post_list = []
        for i in qs:
            post_list.append(
                {
                    'id': i.id,
                    'title': i. title,
                    'content': i.content,
                    'user_id': i.user_id
                }
            )
        return Response({
            'code': 200,
            'message': "分页查询成功",
            'data': post_list
        })